package com.atguigu.gmall.cart.controller;

import com.atguigu.gmall.cart.service.CartService;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.model.cart.CartInfo;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Sanmier
 * @create 2020-12-14 16:43
 */
@Api(tags = "测试购物车接口")
@RequestMapping("api/cart")
@RestController
public class CartApiController {

    @Autowired
    private CartService cartService;


    //  添加购物车  （用户id  商品id  商品数量）
    //  控制器在哪？ item/index.html
    //  window.location.href = 'http://cart.gmall.com/addCart.html?skuId=' + this.skuId + '&skuNum=' + this.skuNum
    //  http://cart.gmall.com/addCart.html 这个应该写在web-all web-all 远程调用当前的对象
    //  url api/cart/addToCart/{skuId}/{skuNum}
    @GetMapping("addToCart/{skuId}/{skuNum}")
    public Result addToCart(@PathVariable Long skuId,
                            @PathVariable Integer skuNum,
                            HttpServletRequest request){

        //获取用户id  网关中已经将用户id放入请求头
        String userId = AuthContextHolder.getUserId(request);
        //添加购物车  登不登陆 都能添加购物网车
        if (StringUtils.isEmpty(userId)){
            //获取一个临时Id
            userId = AuthContextHolder.getUserTempId(request);
        }
        cartService.addToCart(skuId, userId, skuNum);
        //返回数据
        return Result.ok();
    }


    // 通过用户Id 查询购物车列表
    @GetMapping("cartList")
    public Result cartList(HttpServletRequest request) {
        // 获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        // 获取临时用户Id
        String userTempId = AuthContextHolder.getUserTempId(request);
        List<CartInfo> cartList = cartService.getCartList(userId, userTempId);
        return Result.ok(cartList);
    }


    // 更新选中状态
    @GetMapping("checkCart/{skuId}/{isChecked}")
    public Result checkCart(@PathVariable(value = "skuId") Long skuId,
                            @PathVariable(value = "isChecked") Integer isChecked,
                            HttpServletRequest request) {
        // 获取用户Id
        String userId = AuthContextHolder.getUserId(request);
        // update cartInfo set isChecked=? where  skuId = ? and userId=？
        if (StringUtils.isEmpty(userId)) {
            // 未登录
            userId = AuthContextHolder.getUserTempId(request);
        }
        // 调用更新方法
        cartService.checkCart(userId, isChecked, skuId);
        return Result.ok();
    }


    // 删除购物车
    @DeleteMapping("deleteCart/{skuId}")
    public Result deleteCart(@PathVariable Long skuId,
                             HttpServletRequest request){
        //如何让获取skuId  网关中已经将用户id放入到请求头
        String userId = AuthContextHolder.getUserId(request);
        // update cartInfo set isChecked=? where  skuId = ? and userId=？
        //添加购物车  需要注意  登录可以添加  未登录也可以添加
        if (StringUtils.isEmpty(userId)) {
            // 获取一个临时用户id
            userId = AuthContextHolder.getUserTempId(request);
        }
        //删除购物车数据
        cartService.deleteCartInfo(skuId, userId);
        //返回result
        return Result.ok();
    }


    // 根据用户Id 查询购物车列表
    @GetMapping("getCartCheckedList/{userId}")
    public List<CartInfo> getCartCheckedList(@PathVariable String userId){
        return cartService.getCartCheckedList(userId);
    }


    //根据用户id查询实时价格
    @GetMapping("loadCartCache/{userId}")
    public Result loadCartCache(@PathVariable("userId") String userId) {
        cartService.loadCartCache(userId);
        return Result.ok();
    }

}
